/*
	Copyright (c) 2009-2011, Stefan Webb.

	This file is part of SimpleML. Please read the file "license.txt", 
	which contains the conditions under which this software is distributed.
*/

/*!
	\file file.h
	\author Stefan Webb
	\brief Definition of File class
*/

#ifndef FILE_H
#define FILE_H

/*! \brief Enumeration of file open modes. */
enum FileMode {
	NOTSET, READ, WRITE
};

/*! \brief Some useful file functions.
	Inherit a class from File to make them available.*/
class File : public Agent
{
public:
	/*! \brief Default constructor */
	File();

	/*! \brief Constructor with filename passed 
		\param filename File to open.
		\param mode Mode under which to open file. */
	File(const wchar_t* filename, FileMode mode);

	/*! \brief Default destructor */
	~File();

	/*! \brief Initialize variables */
	void initialize();
	
	/*! \brief Opens file
		\param filename Filename to open.
		\param mode Mode under which to open file.
		\return True if operation succeeds, false otherwise. */
	bool open(const wchar_t* filename, FileMode mode);

	/*! \brief Closes file */
	bool close();

	/*! \brief Reads entire file into memory as binary
		\return Array containing file contents. (Program must free the memory to the
		returned pointer with "delete []".) */
	unsigned char* read();

	/*! \brief Reads part of file into memory as binary
		\param size Size of data to read.
		\return Array containing file contents. (Program must free the memory to the
		returned pointer with "delete []".) */
	unsigned char* read(unsigned int size);

	/*! \brief Reads part of file into specified buffer as binary
		\param size Size of data to read.
		\param buffer Address whereto data is read. 
		\return True if operation succeeds, false otherwise. */
	bool read(unsigned int size, char* buffer);

	/*! \brief Compare two file names, which is a case insensitive string compare on Windows.
		\param filename1 First filename.
		\param filename2 Second filename.
		\return True if the filenames are equivalent. False, otherwise. */
	bool compare(const wchar_t* filename1, const wchar_t* filename2);

	/*! \brief Get file size in bytes.
		\return File size in bytes. */
	unsigned int size();

	/*! \brief Save block of memory to file.
		\param memory Pointer to block of memory.
		\param length Length in bytes of memory block.
		\return True if operation succeeds, false otherwise. */
	bool save(unsigned int length, unsigned char* memory);

private:
	/*! \brief Case insensitive string compare
		\param str1 First string.
		\param str2 Second string.
		\return Zero if strings are equivalent. Nonzero, otherwise. */
	int strcasecmp(const wchar_t *str1, const wchar_t *str2);

	/*! \brief fstream for class */
	HANDLE file_;

	/*! \brief Mode that the file was opened under. */
	FileMode mode_;

	/*! \brief Name of opened file. */
	wchar_t* filename_;

	/*! \brief Size of file. */
	unsigned int size_;

};

#endif